<?php

namespace App\Entity;

use App\Event\UserChangeEvent;
use App\EventSubscriber\UserSubscriber;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Symfony\Component\Security\Core\User\UserInterface;
use ApiPlatform\Core\Annotation\ApiProperty;

/**
 * @ApiResource()
 * @ORM\Table(name="users")
 * @ApiFilter(GroupFilter::class, arguments={"parameterName": "_g"})
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface
{
    /**
     * @Groups({"user","parent","home","task"})
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Groups({"user","parent","home","task"})
     * @ORM\Column(type="string", length=180, unique=true,name="name")
     */
    private $name;

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string", nullable=true)
     */
    private $password;

    /**
     * @Groups({"user","home","task"})
     * @ORM\Column(type="string", length=255)
     */
    private $phone;

    /**
     * @Groups({"user","parent","home","task"})
     * @ORM\Column(type="string", length=255)
     */
    private $avatar = "";

    /**
     * @Groups({"parent"})
     * @ORM\ManyToOne(targetEntity=User::class )
     * @ORM\JoinColumn(name="parent_id")
     * @ApiSubresource
     */
    private $parent;

    /**
     * @Groups({"son"})
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="parent")
     */
    private $sons;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $ApiToken;

    /**
     * @Groups({"home"})
     * @ORM\OneToMany(targetEntity="App\Entity\UserAddress", mappedBy="user", orphanRemoval=true)
     */
    private $addresses;

    /**
     * @Groups({"home"})
     * @ORM\OneToMany(targetEntity="App\Entity\UserContact", mappedBy="user", orphanRemoval=true)
     */
    private $contacts;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\UserAuth", mappedBy="user", cascade={"persist", "remove"})
     */
    private $auth;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\UserSearch", mappedBy="user", orphanRemoval=true)
     */
    private $searches;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\UserPunish", mappedBy="user", orphanRemoval=true)
     */
    private $punishes;

    /**
     * @Groups({"home"})
     * @ORM\OneToOne(targetEntity="App\Entity\Shop", mappedBy="user", cascade={"persist", "remove"} )
     */
    private $shop;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Task", mappedBy="user", orphanRemoval=true)
     * @ORM\OrderBy({"id"="DESC"})
     */
    private $tasks;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Appeal", mappedBy="user", orphanRemoval=true)
     */
    private $appeals;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Suggest", mappedBy="user")
     */
    private $suggests;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="who", orphanRemoval=true)
     */
    private $comments;

    /**
     * @ORM\Column(type="datetime",nullable=true)
     */
    private $createAt;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Money", mappedBy="user", orphanRemoval=true)
     */
    private $money;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\MoneyLog", mappedBy="user", orphanRemoval=true)
     */
    private $moneyLogs;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\MoneyStatic", mappedBy="user", orphanRemoval=true)
     */
    private $moneyStatics;

    public function __construct()
    {
        $this->sons = new ArrayCollection();
        $this->addresses = new ArrayCollection();
        $this->contacts = new ArrayCollection();
        $this->searches = new ArrayCollection();
        $this->punishes = new ArrayCollection();
        $this->tasks = new ArrayCollection();
        $this->appeals = new ArrayCollection();
        $this->suggests = new ArrayCollection();
        $this->comments = new ArrayCollection();
        $this->money = new ArrayCollection();
        $this->moneyLogs = new ArrayCollection();
        $this->moneyStatics = new ArrayCollection();
    }


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return (string) $this->name;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        // not needed when using the "bcrypt" algorithm in security.yaml
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getApiToken(): ?string
    {
        return $this->ApiToken;
    }

    public function setApiToken(?string $ApiToken): self
    {
        $this->ApiToken = $ApiToken;

        return $this;
    }

    public function trigger($eventName)
    {
        $event = new UserChangeEvent($this);
        $dispatcher = new EventDispatcher();
        $dispatcher->addSubscriber(new UserSubscriber());
        $dispatcher->dispatch($event,$eventName);
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    public function getAvatar(): ?string
    {
        return $this->avatar;
    }

    public function setAvatar(string $avatar): self
    {
        $this->avatar = $avatar;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|User[]
     */
    public function getSons(): Collection
    {
        return $this->sons;
    }

    public function addSon(User $son): self
    {
        if (!$this->sons->contains($son)) {
            $this->sons[] = $son;
            $son->setParent($this);
        }

        return $this;
    }

    public function removeSon(User $son): self
    {
        if ($this->sons->contains($son)) {
            $this->sons->removeElement($son);
            // set the owning side to null (unless already changed)
            if ($son->getParent() === $this) {
                $son->setParent(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|UserAddress[]
     */
    public function getAddresses(): Collection
    {
        return $this->addresses;
    }

    public function addAddress(UserAddress $address): self
    {
        if (!$this->addresses->contains($address)) {
            $this->addresses[] = $address;
            $address->setUser($this);
        }

        return $this;
    }

    public function removeAddress(UserAddress $address): self
    {
        if ($this->addresses->contains($address)) {
            $this->addresses->removeElement($address);
            // set the owning side to null (unless already changed)
            if ($address->getUser() === $this) {
                $address->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|UserContact[]
     */
    public function getContacts(): Collection
    {
        return $this->contacts;
    }

    public function addContact(UserContact $contact): self
    {
        if (!$this->contacts->contains($contact)) {
            $this->contacts[] = $contact;
            $contact->setUser($this);
        }

        return $this;
    }

    public function removeContact(UserContact $contact): self
    {
        if ($this->contacts->contains($contact)) {
            $this->contacts->removeElement($contact);
            // set the owning side to null (unless already changed)
            if ($contact->getUser() === $this) {
                $contact->setUser(null);
            }
        }

        return $this;
    }

    public function getAuth(): ?UserAuth
    {
        return $this->auth;
    }

    public function setAuth(UserAuth $auth): self
    {
        $this->auth = $auth;

        // set the owning side of the relation if necessary
        if ($auth->getUser() !== $this) {
            $auth->setUser($this);
        }

        return $this;
    }

    /**
     * @return Collection|UserSearch[]
     */
    public function getSearches(): Collection
    {
        return $this->searches;
    }

    public function addSearch(UserSearch $search): self
    {
        if (!$this->searches->contains($search)) {
            $this->searches[] = $search;
            $search->setUser($this);
        }

        return $this;
    }

    public function removeSearch(UserSearch $search): self
    {
        if ($this->searches->contains($search)) {
            $this->searches->removeElement($search);
            // set the owning side to null (unless already changed)
            if ($search->getUser() === $this) {
                $search->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|UserPunish[]
     */
    public function getPunishes(): Collection
    {
        return $this->punishes;
    }

    public function addPunish(UserPunish $punish): self
    {
        if (!$this->punishes->contains($punish)) {
            $this->punishes[] = $punish;
            $punish->setUser($this);
        }

        return $this;
    }

    public function removePunish(UserPunish $punish): self
    {
        if ($this->punishes->contains($punish)) {
            $this->punishes->removeElement($punish);
            // set the owning side to null (unless already changed)
            if ($punish->getUser() === $this) {
                $punish->setUser(null);
            }
        }

        return $this;
    }

    public function getShop(): ?Shop
    {
        return $this->shop;
    }

    public function setShop(Shop $shop): self
    {
        $this->shop = $shop;

        // set the owning side of the relation if necessary
        if ($shop->getUser() !== $this) {
            $shop->setUser($this);
        }

        return $this;
    }

    /**
     * @return Collection|Task[]
     */
    public function getTasks($type = null): Collection
    {
        switch ($type){
            case 1:
                $re = new ArrayCollection;
                foreach ($this->tasks as $task) {
                    if ($task->getType() == 1) {
                        $re->add($task) ;
                    }
                }
                return $re;
            case 2:
                $re = new ArrayCollection;
                foreach ($this->tasks as $task) {
                    if ($task->getType() == 2) {
                        $re->add($task) ;
                    }
                }
                return $re;
            default:
        }

        return $this->tasks;
    }

    public function addTask(Task $task): self
    {
        if (!$this->tasks->contains($task)) {
            $this->tasks[] = $task;
            $task->setUser($this);
        }

        return $this;
    }

    public function removeTask(Task $task): self
    {
        if ($this->tasks->contains($task)) {
            $this->tasks->removeElement($task);
            // set the owning side to null (unless already changed)
            if ($task->getUser() === $this) {
                $task->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Appeal[]
     */
    public function getAppeals(): Collection
    {
        return $this->appeals;
    }

    public function addAppeal(Appeal $appeal): self
    {
        if (!$this->appeals->contains($appeal)) {
            $this->appeals[] = $appeal;
            $appeal->setUser($this);
        }

        return $this;
    }

    public function removeAppeal(Appeal $appeal): self
    {
        if ($this->appeals->contains($appeal)) {
            $this->appeals->removeElement($appeal);
            // set the owning side to null (unless already changed)
            if ($appeal->getUser() === $this) {
                $appeal->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Suggest[]
     */
    public function getSuggests(): Collection
    {
        return $this->suggests;
    }

    public function addSuggest(Suggest $suggest): self
    {
        if (!$this->suggests->contains($suggest)) {
            $this->suggests[] = $suggest;
            $suggest->setUser($this);
        }

        return $this;
    }

    public function removeSuggest(Suggest $suggest): self
    {
        if ($this->suggests->contains($suggest)) {
            $this->suggests->removeElement($suggest);
            // set the owning side to null (unless already changed)
            if ($suggest->getUser() === $this) {
                $suggest->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Comment[]
     */
    public function getComments(): Collection
    {
        return $this->comments;
    }

    public function addComment(Comment $comment): self
    {
        if (!$this->comments->contains($comment)) {
            $this->comments[] = $comment;
            $comment->setWho($this);
        }

        return $this;
    }

    public function removeComment(Comment $comment): self
    {
        if ($this->comments->contains($comment)) {
            $this->comments->removeElement($comment);
            // set the owning side to null (unless already changed)
            if ($comment->getWho() === $this) {
                $comment->setWho(null);
            }
        }

        return $this;
    }

    public function getCreateAt(): ?\DateTimeInterface
    {
        return $this->createAt;
    }

    public function setCreateAt(\DateTimeInterface $createAt): self
    {
        $this->createAt = $createAt;

        return $this;
    }

    /**
     * @return Collection|Money[]
     */
    public function getMoney(): Collection
    {
        return $this->money;
    }

    public function addMoney(Money $money): self
    {
        if (!$this->money->contains($money)) {
            $this->money[] = $money;
            $money->setUser($this);
        }

        return $this;
    }

    public function removeMoney(Money $money): self
    {
        if ($this->money->contains($money)) {
            $this->money->removeElement($money);
            // set the owning side to null (unless already changed)
            if ($money->getUser() === $this) {
                $money->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|MoneyLog[]
     */
    public function getMoneyLogs(): Collection
    {
        return $this->moneyLogs;
    }

    public function addMoneyLog(MoneyLog $moneyLog): self
    {
        if (!$this->moneyLogs->contains($moneyLog)) {
            $this->moneyLogs[] = $moneyLog;
            $moneyLog->setUser($this);
        }

        return $this;
    }

    public function removeMoneyLog(MoneyLog $moneyLog): self
    {
        if ($this->moneyLogs->contains($moneyLog)) {
            $this->moneyLogs->removeElement($moneyLog);
            // set the owning side to null (unless already changed)
            if ($moneyLog->getUser() === $this) {
                $moneyLog->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|MoneyStatic[]
     */
    public function getMoneyStatics(): Collection
    {
        return $this->moneyStatics;
    }

    public function addMoneyStatic(MoneyStatic $moneyStatic): self
    {
        if (!$this->moneyStatics->contains($moneyStatic)) {
            $this->moneyStatics[] = $moneyStatic;
            $moneyStatic->setUser($this);
        }

        return $this;
    }

    public function removeMoneyStatic(MoneyStatic $moneyStatic): self
    {
        if ($this->moneyStatics->contains($moneyStatic)) {
            $this->moneyStatics->removeElement($moneyStatic);
            // set the owning side to null (unless already changed)
            if ($moneyStatic->getUser() === $this) {
                $moneyStatic->setUser(null);
            }
        }

        return $this;
    }
}
